//
//  DMSelectCityVC.m
//  DistributionMallApp
//
//  Created by JHnoplan on 2018/10/19.
//  Copyright © 2018年 JHnoplan. All rights reserved.
//

#import "DMSelectCityVC.h"
#import "DMSelectCityCell.h"

@interface DMSelectCityVC ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSDictionary *mainData;
@property (nonatomic, strong) NSMutableArray *provinceArr;
@property (nonatomic, strong) NSMutableArray *cityArr;
@property (nonatomic, strong) NSMutableArray *countyArr;

@end

@implementation DMSelectCityVC

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self startInit];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(completeSelectArea:) name:@"completeSelectArea" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(completeSelectArea2:) name:@"completeSelectArea2" object:nil];
}
-(void)completeSelectArea:(NSNotification*)noti {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"completeSelectArea2" object:noti.object userInfo:nil];
    [self.navigationController popViewControllerAnimated:NO];
}
-(void)completeSelectArea2:(NSNotification*)noti {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"completeSelectArea3" object:noti.object userInfo:nil];
    [self.navigationController popViewControllerAnimated:NO];
}

-(void)startInit {
    
    self.navigationItem.title = @"选择地址";
    self.view.backgroundColor = DMWhiteColor;
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    //注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"DMSelectCityCell" bundle:nil] forCellReuseIdentifier:@"DMSelectCityCell"];
    
    _provinceArr = [NSMutableArray new];
    _cityArr = [NSMutableArray new];
    _countyArr = [NSMutableArray new];
    //地区数据
    _mainData = [self readLocalFileWithName:@"newDistrict"];
    
    [_provinceArr addObjectsFromArray:_mainData.allKeys];
    
//    MJWeakSelf
//    [self setComplete:^(NSString *str) {
//        if (weakSelf.complete) {
//            weakSelf.complete(str);
//        }
//        [weakSelf.navigationController popViewControllerAnimated:YES];
//    }];
}

// 读取本地JSON文件
- (NSDictionary *)readLocalFileWithName:(NSString *)name {
    // 获取文件路径
    NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"json"];
    // 将文件数据化
    NSData *data = [[NSData alloc] initWithContentsOfFile:path];
    // 对数据进行JSON格式化并返回字典形式
    return [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_flag == 1 || _flag == 2) {
        return _dataArr.count;
    }
    return _provinceArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DMSelectCityCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DMSelectCityCell" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"DMSelectCityCell" owner:self options:nil].firstObject;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (_flag == 1 || _flag == 2) {
        cell.titleLb.text = _dataArr[indexPath.row];
    }else {
        cell.titleLb.text = _provinceArr[indexPath.row];
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (_flag == 1) {
        //选择地址
        NSString *cityStr = _dataArr[indexPath.row];
        NSArray *arr = _dataDict[cityStr];
        [_countyArr removeAllObjects];
        [_countyArr addObjectsFromArray:arr];
        DMSelectCityVC *vc = [DMSelectCityVC new];
        vc.flag = 2;
        vc.dataArr = _countyArr;
        vc.areaStr = [NSString stringWithFormat:@"%@%@",_areaStr,cityStr];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (_flag == 2) {
        //已选完，回到添加地址界面
        NSString *countyStr = _dataArr[indexPath.row];
        _areaStr = [NSString stringWithFormat:@"%@%@",_areaStr,countyStr];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"completeSelectArea" object:_areaStr userInfo:nil];
        [self.navigationController popViewControllerAnimated:YES];
    }else {
        NSString *provinceStr = _provinceArr[indexPath.row];
        NSDictionary *cityDict = _mainData[provinceStr];
        [_cityArr removeAllObjects];
        [_cityArr addObjectsFromArray:cityDict.allKeys];
        DMSelectCityVC *vc = [DMSelectCityVC new];
        vc.flag = 1;
        vc.dataDict = cityDict;
        vc.dataArr = _cityArr;
        vc.areaStr = provinceStr;
        [self.navigationController pushViewController:vc animated:YES];
    }
}


@end
